<?php
class Adaily {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__dochkinday();
				break;
			case 2:
				self::__dochkinlist();
				break;
			case 3:
				self::__dochkinclaim();
				break;
			case 4:
				self::__domissionlist();
				break;
			case 5:
				self::__domissionclaim();
				break;
			case 6:
				self::__dogetcdchest();
				break;
			case 7:
				self::__docdchestclaim();
				break;
			case 8:
				self::__dosharecallback();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//3-10-1判断今日是否签到以及连续签到天数
	private static function __dochkinday() {
		$acl   = acl::getinst('vedio');
		$userid= $acl->user['id'];
		$data  = api_dailysign::sign($userid);
		$symd  = date('Ymd', config::$nowtime);
		$result= array('issign'=>($symd == $data['sdate'])? 1:0, 'day'=>$data['stime']);
		sview::$view->assign_by_ref('datasets', $result);
		sview::$view->display();
	}
	
	//3-10-2获取签到奖励配置列表以及每日签到情况
	private static function __dochkinlist() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array();
		$data   = api_dailysign::sign($userid);
		$list   = model::load('live/signconfig')
			->get_only_list(0, 7, $where, 'id,day,exp,type', 'day', orm::ASC);
		foreach ($list as &$item) {
			$item['status'] = isset($data['award'][$item['day']])? intval($data['award'][$item['day']]):2;
		}
		$result = array('list'=>$list, 'signday'=>$data['stime'] % api_dailysign::LIMITDAYS + 1); 	
		sview::$view->assign_by_ref('datasets', $result);
		sview::$view->display();
	}
	
	//3-10-3执行签到奖励领取
	private static function __dochkinclaim() {
		$day    = isset(sview::$ctrl['day'])? intval(sview::$ctrl['day']):1;
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		//判断是否已经邮箱验证过了
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user || !$user['emailverify']) {
			sview::$view->assign_error(80068, lang::text(80068));
		}
		$sign   = api_dailysign::dosign($userid, $day);
		if (is_numeric($sign)) {//返回数值说明请求错误
			sview::$view->assign_error(80009, lang::text(80009));
		}
		//奖励发放
		$result = api_dailysign::award($userid, $sign);
		$names  = array();
		foreach ($result['list'] as $item) {
			$names[] = $item['name'];
		}
		$datasets = array('type'=>$sign['type'], 'names'=>implode(',', $names));
		sview::$view->assign_by_ref('datasets', $datasets);
		sview::$view->display();
	}
	
	//3-10-4任务信息列表
	private static function __domissionlist() {
		$acl   = acl::getinst('vedio');
		$userid= $acl->user['id'];
		$sid   = isset(sview::$ctrl['studioid'])? intval(sview::$ctrl['studioid']):0;
		$dataset = api_mission::dolist($sid, $userid);
		$static  = static_uri();
		unset($dataset['coin']);
		foreach ($dataset['list'] as &$item) {
			$item['award_icon'] = '';
			$item['award_desc'] = '';
			$item['icon'] = strval($item['icon']);
			if ($item['status'] == 1) {
				$item['achveid'] = 0;
			}
			if ($item['id'] == 3) {
				$item['award_icon'] = $static.'/static/v1.0/images/pocket-coins.png';
				$item['award_desc'] = 'x2';
			} else if ($item['coin'] > 0) {
				$item['award_icon'] = $static.'/static/v1.0/images/pocket-coins.png';
				$item['award_desc'] = sprintf('%d', $item['coin']);
			} else if ($item['exp'] > 0) {
				$item['award_icon'] = $static.'/static/v1.0/images/exp.png';
				$item['award_desc'] = sprintf('%d', $item['exp']);
			} else if (isset($item['gift']) && !empty($item['gift'])) {
				$item['award_icon'] = $item['gift']['icon'];
				$item['award_desc'] = sprintf('%d', $item['gift']['nums']);
			}
			unset($item['giftids'], $item['jump'], $item['status'], $item['type'], $item['gift'], $item['coin'], $item['exp']);
		}
		sview::$view->assign_by_ref('datasets', $dataset);
		sview::$view->display();
	}
	
	//3-10-5执行签到奖励领取
	private static function __domissionclaim() {
		$acl     = acl::getinst('vedio');
		$userid  = $acl->user['id'];
		$achveid = isset(sview::$ctrl['achveid'])? intval(sview::$ctrl['achveid']):0;
		//判断是否已经邮箱验证过了
		$user    = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if ($achveid < 1 || !$user || !$user['emailverify']) {
			sview::$view->assign_error(80068, lang::text(80068));
		}
		$data    = api_mission::doaward($userid, $achveid);
		if (!$data) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$static  = static_uri();
		$list    = array();
		foreach ($data['list'] as $item) {
			if (isset($item['icon']) && isset($item['name'])) {
				$list[] = array('icon'=>$static.$item['icon'], 'title'=>$item['name']);
			}
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//3-10-6 获取CDTime时间
	private static function __dogetcdchest() {
		$acl   = acl::getinst('vedio');
		$userid= $acl->user['id'];
		$result= array('cdtime'=>0, 'hidden'=>1, 'lv'=>1, 'hash'=>'');
		$user  = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if ($user && $user['emailverify']) {
			$result = api_dailychest::get($userid);
		}
		sview::$view->assign_by_ref('datasets', $result);
		sview::$view->display();
	}
	
	//3-10-8 分享任务完成之后的回调
	private static function __dosharecallback() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$sid    = isset(sview::$ctrl['studioid'])? intval(sview::$ctrl['studioid']):0;
		if ($sid > 0) {
			api_mission::share_mission($userid, $sid);
		}
		sview::$view->display();
	}
	
	//3-10-7 CDTime到期领奖
	private static function __docdchestclaim() {
		$acl   = acl::getinst('vedio');
		$userid= $acl->user['id'];
		$hash  = isset(sview::$ctrl['hash'])? sview::$ctrl['hash']:'';
		$result= api_dailychest::sign($userid, $hash);
		if (is_numeric($result)) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		sview::$view->assign_by_ref('datasets', $result);
		sview::$view->display();
	}
}